package com.eazytec.efs.common;

import com.zhy.http.okhttp.callback.StringCallback;
import okhttp3.Call;
import okhttp3.Request;
import org.apache.log4j.Logger;

/**
 * 上传 回调函数
 *
 * @author Zhaozx
 * @create 2017-10-13 8:39
 **/
public class MyStringCallback extends StringCallback {

    static Logger logger = Logger.getLogger(MyStringCallback.class);

    private ResultMsg resultMsg;

    public ResultMsg getResultMsg() {
        return resultMsg;
    }

    public void setResultMsg(ResultMsg resultMsg) {
        this.resultMsg = resultMsg;
    }


    public MyStringCallback(ResultMsg resultMsg) {
    }

    @Override
    public void onBefore(Request request, int id) {
        logger.info("Prapare upload start ...");
        logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }

    @Override
    public void onAfter(int id) {
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        logger.info("Finish upload ...");
    }

    @Override
    public void onError(Call call, Exception e, int id) {
        e.printStackTrace();
        resultMsg.setCode(Constants.fail_code);
        resultMsg.setMsg(e.getMessage());
        logger.error("!!!ERROR: " + e.getMessage());
    }

    @Override
    public void onResponse(String response, int id) {
        logger.info("Response complete");
        switch (id) {
            case 100:
                logger.info("http");
                break;
            case 101:
                logger.info("https");
                break;
            case 108://列表中显示数据
                if (response != null) {
                    logger.info("response:" + response.toString());
                }
                break;
        }
    }

    @Override
    public void inProgress(float progress, long total, int id) {
        logger.info("UploadProgress: " + (int) (100 * progress) + " %");
    }
}

